import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _class;

/**
 * 语言切换
 */
import React from 'react';
import { Select } from 'antd';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import classNames from 'classnames';
var DefaultLanguageSelect = (_dec = Bind(), (_class = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(DefaultLanguageSelect, _React$PureComponent);

  var _super = _createSuper(DefaultLanguageSelect);

  function DefaultLanguageSelect() {
    _classCallCheck(this, DefaultLanguageSelect);

    return _super.apply(this, arguments);
  }

  _createClass(DefaultLanguageSelect, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var querySupportLanguage = this.props.querySupportLanguage;
      querySupportLanguage();
    }
  }, {
    key: "handleLanguageSelectChange",
    value: function handleLanguageSelectChange(language) {
      var _this$props = this.props,
          onChangeLanguage = _this$props.onChangeLanguage,
          onUpdateDefaultLanguage = _this$props.onUpdateDefaultLanguage;
      Promise.all([onChangeLanguage(language), onUpdateDefaultLanguage({
        language: language
      })]).then(function () {
        window.location.reload();
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          language = _this$props2.language,
          _this$props2$supportL = _this$props2.supportLanguage,
          supportLanguage = _this$props2$supportL === void 0 ? [] : _this$props2$supportL,
          _this$props2$loading = _this$props2.loading,
          loading = _this$props2$loading === void 0 ? false : _this$props2$loading,
          className = _this$props2.className;
      return /*#__PURE__*/React.createElement(Select, {
        size: "small",
        className: classNames('select-no-border', 'default-language-select', className),
        value: language,
        onChange: this.handleLanguageSelectChange,
        disabled: loading
      }, supportLanguage.map(function (locale) {
        return /*#__PURE__*/React.createElement(Select.Option, {
          key: locale.code,
          value: locale.code,
          className: "cpaas-normal-header-container-language"
        }, locale.name);
      }));
    }
  }]);

  return DefaultLanguageSelect;
}(React.PureComponent), (_applyDecoratedDescriptor(_class.prototype, "handleLanguageSelectChange", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleLanguageSelectChange"), _class.prototype)), _class));
export default connect(function (_ref) {
  var _ref$global = _ref.global,
      global = _ref$global === void 0 ? {} : _ref$global,
      _ref$loading = _ref.loading,
      loading = _ref$loading === void 0 ? {
    effects: {}
  } : _ref$loading;
  return {
    supportLanguage: global.supportLanguage,
    // 可供切换的语言
    language: global.language,
    // 当前的语言
    loading: loading.effects['global/changeLanguage'] || loading.effects['global/updateDefaultLanguage'] || loading.effects['global/querySupportLanguage']
  };
}, function (dispatch) {
  return {
    onChangeLanguage: function onChangeLanguage(payload) {
      return dispatch({
        type: 'global/changeLanguage',
        payload: payload
      });
    },
    onUpdateDefaultLanguage: function onUpdateDefaultLanguage(payload) {
      return dispatch({
        type: 'global/updateDefaultLanguage',
        payload: payload
      });
    },
    querySupportLanguage: function querySupportLanguage() {
      return dispatch({
        type: 'global/querySupportLanguage'
      });
    }
  };
})(DefaultLanguageSelect);